cmake_minimum_required(VERSION 3.5)

project(minesweeper_client_qt VERSION 0.1 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# TODO: Windows 上未定义此宏时，asio在处理消息时会报段错误
add_definitions(-DBOOST_ASIO_DISABLE_BUFFER_DEBUGGING=1)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Network)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Network)

find_package(protobuf CONFIG REQUIRED)
find_package(Boost REQUIRED)

file(GLOB_RECURSE ui_files CONFIGURE_DEPENDS
    "${CMAKE_SOURCE_DIR}/*.ui")

file(GLOB_RECURSE src_files CONFIGURE_DEPENDS
    "${CMAKE_SOURCE_DIR}/src/*.cpp"
    "${CMAKE_SOURCE_DIR}/src/*.cc"
    "${CMAKE_SOURCE_DIR}/src/*.h")

file(GLOB_RECURSE qrc_files CONFIGURE_DEPENDS
    "${CMAKE_SOURCE_DIR}/*.qrc")

list(REMOVE_ITEM src_files "${CMAKE_SOURCE_DIR}/src/main.cpp")

qt_add_library(minesweeper_client_qt_lib STATIC
    MANUAL_FINALIZATION
    ${ui_files}
    ${src_files}
    ${qrc_files}
)

set(include_dirs
    ${Boost_INCLUDE_DIRS}
    ${CMAKE_SOURCE_DIR}/src
    ${CMAKE_SOURCE_DIR}/src/net
    ${CMAKE_SOURCE_DIR}/src/net/network_message
    ${CMAKE_SOURCE_DIR}/src/net/network_message/protobuf
    ${CMAKE_SOURCE_DIR}/src/util)

target_include_directories(minesweeper_client_qt_lib PRIVATE ${include_dirs})
target_link_libraries(minesweeper_client_qt_lib PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
target_link_libraries(minesweeper_client_qt_lib PRIVATE Qt${QT_VERSION_MAJOR}::Network)
target_link_libraries(minesweeper_client_qt_lib PRIVATE
    protobuf::libprotoc protobuf::libprotobuf protobuf::libprotobuf-lite)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(minesweeper_client_qt
        MANUAL_FINALIZATION
        ${ui_files}
        "${CMAKE_SOURCE_DIR}/src/main.cpp"
        ${qrc_files}
    )
# Define target properties for Android with Qt 6 as:
#    set_property(TARGET minesweeper_client_qt APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
#                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
    if(ANDROID)
        add_library(minesweeper_client_qt SHARED
            ${PROJECT_SOURCES}
        )
# Define properties for Android with Qt 5 after find_package() calls as:
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
    else()
        add_executable(minesweeper_client_qt
            ${PROJECT_SOURCES}
        )
    endif()
endif()

target_link_libraries(minesweeper_client_qt PRIVATE minesweeper_client_qt_lib)
target_link_libraries(minesweeper_client_qt PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
target_link_libraries(minesweeper_client_qt PRIVATE Qt${QT_VERSION_MAJOR}::Network)

target_link_libraries(minesweeper_client_qt PRIVATE
    protobuf::libprotoc protobuf::libprotobuf protobuf::libprotobuf-lite)

target_include_directories(minesweeper_client_qt PRIVATE ${include_dirs})
target_include_directories(minesweeper_client_qt PRIVATE "${CMAKE_SOURCE_DIR}/build/minesweeper_client_qt_lib_autogen/include")

# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
if(${QT_VERSION} VERSION_LESS 6.1.0)
  set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.example.minesweeper_client_qt)
endif()
set_target_properties(minesweeper_client_qt PROPERTIES
    ${BUNDLE_ID_OPTION}
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

include(GNUInstallDirs)
install(TARGETS minesweeper_client_qt
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(minesweeper_client_qt)
endif()

# 开发者测试
find_package(GTest CONFIG REQUIRED)

file(GLOB_RECURSE dt_src_files CONFIGURE_DEPENDS
    "${CMAKE_SOURCE_DIR}/test/*.cpp"
)

qt_add_executable(minesweeper_client_test
    MANUAL_FINALIZATION
    ${dt_src_files})
target_link_libraries(minesweeper_client_test PRIVATE minesweeper_client_qt_lib GTest::gtest GTest::gtest_main GTest::gmock GTest::gmock_main)
target_include_directories(minesweeper_client_test PRIVATE ${include_dirs})
target_include_directories(minesweeper_client_test PRIVATE "${CMAKE_SOURCE_DIR}/build/minesweeper_client_qt_lib_autogen/include")